{% load querystring from django_tables2 %}
{% load table_config_button from helpers %}

<table id="job_accordion" {% if table.attrs %} {{ table.attrs.as_html }}{% endif %}>
    {% if table.show_header %}
        <thead>
            <tr>
                {% for column in table.columns %}
                    {% if not forloop.last %}
                        {% if column.orderable %}
                            <th {{ column.attrs.th.as_html }}><a href="{% querystring table.prefixed_order_by_field=column.order_by_alias.next %}">{{ column.header }}</a></th>
                        {% else %}
                            <th {{ column.attrs.th.as_html }}>{{ column.header }}</th>
                        {% endif %}
                    {% else %}
                        <th {{ column.attrs.th.as_html }}>
                            {% table_config_button table %}
                        </th>
                    {% endif %}
                {% endfor %}
            </tr>
        </thead>
    {% endif %}
    {% load helpers %}
    <tbody>
        {% for row in table.page.object_list|default:table.rows %}
            {% ifchanged row.record.grouping %}
                <tr>
                    <th class="position-relative" colspan="{{ table.columns|length }}">
                        <button
                            aria-expanded="true"
                            class="btn float-none fs-4 stretched-link nb-collapse-toggle"
                            data-bs-target=".collapseme-{{ row.record.grouping|slugify }}"
                            data-bs-toggle="collapse"
                            name="grouping.{{ row.record.grouping }}"
                            type="button"
                        >
                            <span aria-hidden="true" class="mdi mdi-chevron-down me-4"></span><!--
                            -->{{ row.record.grouping|bettertitle }}
                        </button>
                    </th>
                </tr>
            {% endifchanged %}
            <tr class="collapseme-{{ row.record.grouping|slugify }}{% if not perms.extras.run_job or not row.record.runnable %} disabled{% endif %} collapse show nb-transition-none" {{ row.attrs.as_html }}>
                {% for column, cell in row.items %}
                    <td {{ column.attrs.td.as_html }}>{{ cell }}</td>
                {% endfor %}
            </tr>
        {% empty %}
            {% if table.empty_text %}
                <tr>
                    <td colspan="{{ table.columns|length }}" class="text-center text-secondary">&mdash; {{ table.empty_text }} &mdash;</td>
                </tr>
            {% endif %}
        {% endfor %}
    </tbody>
    {% if table.has_footer %}
        <tfoot>
            <tr>
                {% for column in table.columns %}
                    <td>{{ column.footer }}</td>
                {% endfor %}
            </tr>
        </tfoot>
    {% endif %}
</table>
